<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {
  getBrandList,
  addPackage,
  editPackage,
  getSeriesListByBrand,
  getModelListBySeries,
  getModelData,
  queryBrandDictOwner,
  querySeriesDict,
  getPackageListByModel,
  addOption,
  editOption
} from '@/api/common/cars/brandcarsModels';
import {getPackageList} from '@/api/common/cars/masterDataCars';

import { testText64,systemModeCode } from '@/utils/validate';
import Cookies from 'js-cookie';
export default {
  name: 'SeriesModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
    };
  },
  created() {
    this.init();
    this.getDictData();
  },
  methods: {
    async init() {
      if (this.type === 'edit') {
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });


      }else{
        this.formList.find(item=>item.fieldName=="brandId").disabled=false
        this.formList.find(item=>item.fieldName=="seriesId").disabled=false
        this.formList.find(item=>item.fieldName=="modelId").disabled=false
        this.formList.find(item=>item.fieldName=="packageId").disabled=false
        this.formList.find(item=>item.fieldName=="optionCode").disabled=false
      }
      // console.log(this.formList,"this.formList")
      let brandId = this.formList.find(item=>item.fieldName=="brandId").initialValue
      if(brandId){
        const resSeries = await querySeriesDict(brandId);
        if (resSeries.resultCode === 200) {
          // console.log(resSeries,"this.resSeries")
          this.formList.find(item=>item.fieldName=="seriesId").itemList = resSeries.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
        }
      }

      let seriesId = this.formList.find(item=>item.fieldName=="seriesId").initialValue
      if(brandId && seriesId){
        const resModel = await getModelListBySeries(brandId,seriesId);
        if (resModel.resultCode === 200) {
          // console.log(resModel,"this.resModel")
          this.formList.find(item=>item.fieldName=="modelId").itemList = resModel.data.map(x => ({ value: x.modelId, text: x.modelName }));
        }
      }

      let modelId = this.formList.find(item=>item.fieldName=="modelId").initialValue
      if(brandId && seriesId && modelId){
        const resPackage = await getPackageListByModel(brandId,seriesId,modelId);
        if (resPackage.resultCode === 200) {
          // console.log(resModel,"this.resModel")
          this.formList.find(item=>item.fieldName=="packageId").itemList = resPackage.data.map(x => ({ value: x.packageId, text: x.configName }));
        }
      }

    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '品牌名称',
          fieldName: 'brandId',
          placeholder: '请选择',
          itemList: [],
          disabled:true,
          change: val => {
            this.changeBrandSelect(val);
          },
          rules: [{ required: true, message: '请选择品牌名称', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '车系名称',
          fieldName: 'seriesId',
          placeholder: '请选择',
          itemList: [],
          disabled:true,
          change: val => {
            console.log(val,"val")
            this.changeSeriesSelect(val);
          },
          rules: [{ required: true, message: '请选择车系名称', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '车型名称',
          fieldName: 'modelId',
          placeholder: '请选择',
          itemList: [],
          disabled:true,
          change: val => {
            console.log(val,"val")
            this.changeModelSelect(val);
          },
          rules: [{ required: true, message: '请选择车型名称', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '配置名称',
          fieldName: 'packageId',
          placeholder: '请选择',
          itemList: [],
          disabled:true,
          rules: [{ required: true, message: '请选择配置名称', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '选装代码',
          fieldName: 'optionCode',
          disabled:true,
          placeholder: '请输入...',
          maxlength:64,
          rules: [
            { required: true, message: '请输入选装代码', trigger: 'blur' },
            { validator: systemModeCode, trigger: 'blur'}
          ]
        },
        {
          type: 'INPUT',
          label: '选装名称',
          fieldName: 'optionName',
          placeholder: '请输入...',
          maxlength:64,
          rules: [{ required: true, message: '请输入选装名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          disabled:true,
          initialValue:10451001,
          placeholder: '请选择',
          itemList: this.createDictList('1045'),
          rules: [{ required: true, message: '请选择车辆来源', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择有效性', trigger: 'change' }]
        }
      ];
    },
    async getDictData() {
      const res = await queryBrandDictOwner();
      if (res.resultCode === 200) {
        this.formList[0].itemList = res.data.map(x => ({ value: x.brandId, text: x.brandName }));
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {

      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        console.log(this.saveData,"this.saveData",this.rowData)
        this.saveData.recordVersion = this.rowData.recordVersion
        if(this.type === 'edit'){
            const res = await editOption(this.saveData,this.rowData.optionId);
            if (res.resultCode === 200) {
              this.closeDialog('refresh');
            }
        }else{
            const res = await addOption(this.saveData);
            if (res.resultCode === 200) {
              this.closeDialog('refresh');
            }
        }

      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    async changeBrandSelect(brandsid) {
      if (!brandsid) {
        this.formList.find(item=>item.fieldName=="seriesId").initialValue = '';
        this.formList.find(item=>item.fieldName=="seriesId").itemList = [];
        this.formList.find(item=>item.fieldName=="modelId").initialValue = '';
        this.formList.find(item=>item.fieldName=="modelId").itemList = [];
        this.formList.find(item=>item.fieldName==='packageId').initialValue = '';
        this.formList.find(item=>item.fieldName==='packageId').itemList = [];
      } else {
        const res = await querySeriesDict(brandsid);
        if (res.resultCode === 200) {
          let seriesId = this.formList.find(item=>item.fieldName=="seriesId").initialValue
          if (seriesId) {
            this.formList.find(item=>item.fieldName=="seriesId").initialValue = '';
          }
          let modelId = this.formList.find(item=>item.fieldName=="modelId").initialValue
          if (modelId) {
            this.formList.find(item=>item.fieldName=="modelId").initialValue = '';
            this.formList.find(item=>item.fieldName=="modelId").itemList = [];
          }
          this.formList.find(item=>item.fieldName=="seriesId").itemList = res.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
        }
      }
    },
    async changeSeriesSelect(seriessid) {
      const brandsid = this.formList[0].initialValue;
      if (!brandsid || !seriessid) {
        this.formList.find(item=>item.fieldName=="modelId").initialValue = '';
        this.formList.find(item=>item.fieldName=="modelId").itemList = [];
        this.formList.find(item=>item.fieldName==='packageId').initialValue = '';
        this.formList.find(item=>item.fieldName==='packageId').itemList = [];
      } else {
        console.log(res,"res")
        const res = await getModelListBySeries(brandsid, seriessid,'');

        if (res.resultCode === 200) {
          let modelId = this.formList.find(item=>item.fieldName=="modelId").initialValue
          if (modelId) {
            this.formList.find(item=>item.fieldName=="modelId").initialValue = '';
          }
          this.formList.find(item=>item.fieldName=="modelId").itemList = res.data.map(x => ({ value: x.modelId, text: x.modelName }));
        }
      }
    },
    async changeModelSelect(modelId) {
      const brandsid = this.formList.find(item=>item.fieldName==='brandId').initialValue;
      const seriessid = this.formList.find(item=>item.fieldName==='seriesId').initialValue;
      if (!brandsid || !seriessid || !modelId) {
        this.formList.find(item=>item.fieldName=="packageId").initialValue = '';
        this.formList.find(item=>item.fieldName=="packageId").itemList = [];
      } else {
        console.log(res,"res")
        const res = await getPackageListByModel(brandsid, seriessid, modelId);

        if (res.resultCode === 200) {
          let packageId = this.formList.find(item=>item.fieldName=="packageId").initialValue
          if (packageId) {
            this.formList.find(item=>item.fieldName=="packageId").initialValue = '';
          }
          this.formList.find(item=>item.fieldName=="packageId").itemList = res.data.map(x => ({ value: x.packageId, text: x.configName }));
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
</style>
